/*
 * Copyright 2025 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import ".."
import "../configitem"
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15
import org.ukui.quick.items 1.0 as UkuiItems
import org.ukui.quick.platform 1.0

Window {
    id: window

    property int dragAreaHeight: 40
    property int itemHeight: 36
    property int dragX: 0
    property int dragY: 0
    property bool dragging: false

    function cancelAction() {
        imContent.showWarning = false;
    }

    function saveAction() {
        layoutManager.joinImEntries(imContent.imName);
        layoutManager.save();
        imModel.addInputMethod(imContent.imName);
        imModel.updateCurrentGroupInputMethodList();
        imContent.showWarning = false;
    }

    function handleDragStarted(posX, posY) {
        dragX = posX;
        dragY = posY;
        dragging = true;
    }

    function handleDragging(posX, posY) {
        if (dragging) {
            window.x += posX - dragX;
            window.y += posY - dragY;
        }
    }

    function handleCloseClicked() {
        imContent.showWarning = false;
    }

    function handleDragEnd() {
        dragging = false;
    }

    width: 440
    height: 200
    modality: Qt.ApplicationModal
    flags: Qt.FramelessWindowHint
    color: "transparent"

    UkuiItems.Background {
        id: imNotMatchWarnBackGround

        anchors.fill: parent
        color: Theme.base()
        radius: 6
        clip: true
        Theme.onPaletteChanged: {
            color = Theme.base();
        }
        border.width: 1
        border.color: Qt.rgba(0, 0, 0, 0.1)

        CustomHeader {
            id: customHeader

            width: parent.width
            height: dragAreaHeight
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.leftMargin: 8
            anchors.rightMargin: 4
            headerTitle: qsTr("System Keyboard Layout")
            z: 1
            onDragStarted: {
                handleDragStarted(posX, posY);
            }
            onDragging: {
                handleDragging(posX, posY);
            }
            onDragEnded: {
                handleDragEnd();
            }
            onCloseClicked: {
                handleCloseClicked();
            }
        }

        ColumnLayout {
            anchors.top: customHeader.bottom
            anchors.topMargin: 16
            width: parent.width
            spacing: 16

            UkuiItems.StyleText {
                id: warningText

                Layout.fillWidth: true
                wrapMode: Text.Wrap
                Layout.leftMargin: 24
                Layout.rightMargin: 24
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                text: qsTr("Your currently configured input method does not match your selected layout, do you want to add the corresponding input method for the layout?")
            }

            UkuiItems.Background {
                Layout.fillWidth: true
                height: 1
                Layout.leftMargin: imNotMatchWarnBackGround.border.width
                Layout.rightMargin: imNotMatchWarnBackGround.border.width
                Layout.preferredHeight: 1
                color: Theme.button()
                Theme.onPaletteChanged: {
                    color = Theme.button();
                }
            }

            RowLayout {
                Layout.fillWidth: true
                Layout.leftMargin: 24
                Layout.rightMargin: 24
                spacing: 8

                Item {
                    Layout.fillWidth: true
                }

                CustomButton {
                    width: 88
                    height: 36
                    labelText: qsTr("Cancel")
                    onCustomButtonClicked: cancelAction()
                }

                CustomButton {
                    width: 88
                    height: 36
                    enableHighlight: true
                    labelText: qsTr("OK")
                    Layout.alignment: Qt.AlignRight
                    onCustomButtonClicked: saveAction()
                }

            }

        }

    }

}
